import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/pages/home/widget/home_station_info_view.dart';
import 'package:flutter_smart_heating/service/auth_service.dart';
import 'package:flutter_smart_heating/ui/ui.dart';

class UserHeaderView extends StatelessWidget {
  const UserHeaderView({super.key, this.user, this.isDark = false});
  final UserData? user;
  final bool isDark;
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(
        left: UiTheme.spacingXX,
        right: UiTheme.spacingXX,
        top: UiTheme.toolbarSpacing,
      ),
      decoration: UiTheme(context).match(
        light: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              UiTheme.primary,
              Theme.of(context).scaffoldBackgroundColor,
            ],
            stops: const [0.12, 1],
          ),
        ),
        dark: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              const Color.fromARGB(255, 10, 28, 86),
              Theme.of(context).scaffoldBackgroundColor,
            ],
            stops: const [0.12, 1],
          ),
        ),
      ),
      child: SafeArea(
        top: true,
        child: Stack(
          children: [
            Container(
              padding: const EdgeInsets.all(UiTheme.spacingX),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(UiTheme.boxRadiusX),
                color: UiTheme.of(context).panelBackground,
                border: UiTheme.of(context).panelBorder,
                image: UiTheme(context).match(
                  light: const DecorationImage(
                    fit: BoxFit.cover,
                    image: AssetImage('res/images/splash.png'),
                  ),
                ),
                boxShadow: UiTheme(context).match(
                  light: [
                    BoxShadow(
                        color: Colors.grey.withOpacity(0.1), blurRadius: 10)
                  ],
                ),
              ),
              child: Column(
                children: [
                  Row(
                    children: [
                      Opacity(
                        opacity: UiTheme(context).match(light: 1.0, dark: 0.7),
                        child: UiAvatar(
                          size: 75,
                          url: user?.avatar,
                          border: Border.all(
                            color: Colors.white.withOpacity(0.2),
                            width: 2,
                          ),
                        ),
                      ),
                      const SizedBox(width: UiTheme.spacingX),
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              user?.name ?? '智慧供热',
                              style: const TextStyle(
                                fontWeight: FontWeight.bold,
                                fontSize: 20,
                              ),
                            ),
                            const SizedBox(height: 5),
                            Text(
                              user?.intro ?? '欢迎使用智慧供热',
                              style: TextStyle(
                                fontSize: 14,
                                color: UiTheme(context).infoColor,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: UiTheme.spacingX),
                  const HomeStationInfoView(key: ValueKey('user')),
                ],
              ),
            ),
            Align(
              alignment: Alignment.topRight,
              child: UiTap(
                onTap: () {
                  Ui.alert(
                      '当前供热季距结束还剩余${AuthService.instance.user.remainingDays}天');
                },
                child: Container(
                  padding:
                      const EdgeInsets.symmetric(horizontal: 10, vertical: 2),
                  decoration: BoxDecoration(
                    color: UiTheme.primary.withOpacity(0.9),
                    borderRadius: const BorderRadius.only(
                      topRight: Radius.circular(10),
                      bottomLeft: Radius.circular(10),
                    ),
                  ),
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      const Icon(
                        UiIcons.clock,
                        size: 13,
                        color: Colors.white,
                      ),
                      const SizedBox(width: 3),
                      Text(
                        '${AuthService.instance.user.remainingDays}天',
                        style: const TextStyle(
                          color: Colors.white,
                          fontSize: 12,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
